package com.hexiang.service.impl;

import com.hexiang.domain.User;
import com.hexiang.response.CommonResponse;
import com.hexiang.service.TaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class TaskServiceImpl implements TaskService {

    @Override
    public String execute() {
        try {
            log.info("taskStart");
            log.info("this is rpc request");
            Thread.sleep(200);
            log.info("taskEnd");
            return "Task finished";
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
        return "";
    }

    @Override
    public CommonResponse execute(String name) {
        try {
            log.info("taskStart");
            log.info("this is rpc request");
            Thread.sleep(200);
            log.info("taskEnd");

            return CommonResponse.builder().success(true).data(User.builder().id(1L).name(name).age(20).build()).code("1001").message("succ").build();
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
        return CommonResponse.builder().success(false).build();
    }

    @Override
    public String timeout() {
        try {
            log.info("taskStart");
            log.info("this is rpc request");
            Thread.sleep(2000);
            log.info("taskEnd");
            return "Task Timeout";
        } catch (InterruptedException e) {
            log.error("interrupt");
            Thread.currentThread().interrupt();
        }
        return "timeOut";
    }

    @Override
    public CommonResponse executeErr() {
        log.info("executeErrStart");
        throw new RuntimeException("this is RuntimeException");
    }
}